import api from '@/api'
import storage from '../../utils/storage'
const user ={
    namespaced: true,
    state:{
        info:{},
        location:storage.getItem('location')||{},
        addressList:[]
    },
    getters: {
        selectAddress(state){
            let address={}
             state.addressList.forEach(item=>{
                if(parseInt(item.defaultActive)===1){
                    address=item
                }
            })
            return address
        }
    },
    mutations:{
        SET_USER_INFO(state,payload){
            state.info=payload
        },
        SET_LOCATION(state,payload){
            storage.setItem('location',payload,1000*60*60*3)
            state.location=payload
         },
         SET_ADDRESS_LIST(state,payload){
             state.addressList=payload
         }
    },
    actions:{
        async getUserInfo({commit,dispatch}){
            const res =await api.user.info()
            dispatch('getAddress')
            dispatch('car/getCarList',null,{root:true})
            commit('SET_USER_INFO',res.data)
        },
        async loginDo({dispatch},payload){
            await api.user.login(payload)
            dispatch('getUserInfo')
        },
        async getAddress({commit}){
            const res=await api.user.address()
            commit('SET_ADDRESS_LIST',res.data)
        }
    },
    modules:{
        
    }
}
export default{
    user
}